{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器编号</label>
            <div class="layui-input-4">
                <input type="text" name="control_id" value="{$info.control_id}" lay-verify="required"
                    placeholder="请输入控制器编号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器全称</label>
            <div class="layui-input-4">
                <input type="text" name="control_longname" value="{$info.control_shortname}" lay-verify="required"
                    placeholder="请输入控制器全称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器简称</label>
            <div class="layui-input-4">
                <input type="text" name="control_shortname" value="{$info.control_longname}" lay-verify="required"
                    placeholder="请输入控制器简称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">联动控制</label>
            <div class="layui-input-block">
                {volist name="control_ids" id="vo" empty="无控制可选择"}
                    <input type="checkbox" name="{$vo.control_id}" lay-skin="primary" title="{$vo.control_shortname}" }>
                {/volist}
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择大棚</label>
            <div class="layui-input-4">
                <select name="house_id" lay-verify="required" lay-filter="hid">
                    <option value="{$info.house_id}">{$info.house_name}</option>
                    {volist name='house' id='vo'}
                    <option value="{$vo.id}">{$vo.house_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择网关</label>
            <div class="layui-input-4">
                <select name="gateway_id" lay-verify="required" id="gateway_id" lay-filter="gateway">
                    <option value="{$info.gateway_id}">{$info.gateway}</option>
                </select>
            </div>
        </div>
			<div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">关闭状态图片</label>
            <input type="hidden" name="close_imgUrl"  value="{$info.close_imgUrl}" id="close_imgUrl">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="sensor_img1"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" src="{$info.close_imgUrl}" id="sensor1">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">开启状态图片</label>
            <input type="hidden" name="open_imgUrl"   value="{$info.open_imgUrl}" id="open_imgUrl">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="sensor_img2"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list"> 
                        <img class="layui-upload-img"  src="{$info.open_imgUrl}"  id="sensor2">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">是否故障</label>
            <div class="layui-input-4">
                <input type="radio" name="is_bad" value="1" title="正常" {if $info.is_bad==1} checked {/if}> <input
                    type="radio" name="is_bad" value="2" title="故障" {if $info.is_bad==2} checked {/if}> </div> </div>
                    <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" name="id" value="{$id}">
                    <input type="hidden" name="id" value="{$id}">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                    <a onclick="navigate_back()" class="layui-btn layui-btn-primary">{:lang('back')}</a>
                </div>
            </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer','upload','element'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,upload = layui.upload,element = layui.element;
        var control_relation = {$control_relation|raw};
        console.log(control_relation);
        $(".layui-form-checkbox").each(function(i,o){
            for (let i = 0; i < control_relation.length; i++) {
                if($(this).prev('input').attr('name') == control_relation[i]){
                    $(this).addClass('layui-form-checked');
                }
            }
            
        });
				var uploadInst = upload.render({
            elem: '#sensor_img1'
            ,url: '{:url("UpFiles/upload")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#sensor1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res);
                //上传成功
                if(res.code>0){
                    $('#close_imgUrl').val(res.url);
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
		var uploadInst2 = upload.render({
            elem: '#sensor_img2'
            ,url: '{:url("UpFiles/upload")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#sensor2').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res);
                //上传成功
                if(res.code>0){
                    $('#open_imgUrl').val(res.url);
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        form.on('select(hid)', function (data) {
            $.ajax({
                url: '{:url("devControllerEdit")}',
                type: 'post',
                data: { 'house_id': data.value },
                success: function (res) {
                    var opt = '';
                    if (res.length > 0) {
                        for (var i = 0; i < res.length; i++) {
                            opt += '<option value="' + res[i]['id'] + '">' + res[i]['gateway'] + '</option>';
                        }
                    }
                    $("#gateway_id").html(opt);
                    form.render('select');
                }
            });
        });
        form.on('submit(submit)', function (data) {

            var arr = new Array();
            $(".layui-form-checked").each(function () {
                arr.push($(this).prev('input').attr('name'));
            });
            control_relation = arr.join(',')

            loading = layer.load(1, { shade: [0.1, '#fff'] });
            $.post('{:url("devControllerEdit")}', { control_relation: control_relation, control_id: data.field.control_id, gateway_id: data.field.gateway_id, id: data.field.id, is_bad: data.field.is_bad, control_shortname: data.field.control_shortname, control_longname: data.field.control_longname,close_imgUrl:$("#close_imgUrl").val(),open_imgUrl:$("#open_imgUrl").val() }, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, { time: 1800, icon: 1 }, function () {
                        window.parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg, { time: 1800, icon: 2 });
                }
            });
        });
    });
    function navigate_back() {
        //parent.location.href="index";
        parent.layer.closeAll();
    }
</script>